#!/bin/sh
if pgrep -x "cmus" >/dev/null; then
	info=$(cmus-remote -Q)
	#echo "$info"
	#status=$(echo "$info" | grep -v "set " | grep -v "tag " | grep "status " | cut -d ' ' -f 2)
	status=$(cmus-remote -Q | grep -v "set " | grep -v "tag " | grep "status " | cut -d ' ' -f 2)
	if [ "$status" = "playing" ] || [ "$status" = "paused" ] || [ "$status" = "stopped" ]; then
		title=$(echo "$info" | grep -v 'set ' | grep "file " | awk -F'/' '{print $NF}')
		artist=$(echo "$info" | grep -v 'set ' | grep " artist " | cut -d ' ' -f 3-)
		position=$(echo "$info" | grep -v "set " | grep -v "tag " | grep "position " | cut -d ' ' -f 2)
		duration=$(echo "$info" | grep -v "set " | grep -v "tag " | grep "duration " | cut -d ' ' -f 2)
		#echo "$title, $artist, $position, $duration"
		$info_string
		if [ "$artist" ] || [ "$title" ]; then
			if [ "$duration" -ge 0 ]; then
				pos_minutes=$(printf "%02d" $((position / 60)))
				pos_seconds=$(printf "%02d" $((position % 60)))

				dur_minutes=$(printf "%02d" $((duration / 60)))
				dur_seconds=$(printf "%02d" $((duration % 60)))

				info_string="| $pos_minutes:$pos_seconds / $dur_minutes:$dur_seconds"
			fi

      # Проверяем, состоит ли строка более чем из 10 элементов
      if [ ${#title} -gt 10 ]; then
      # Если да, берём первые 10 элементов и добавляем "..." в конец новой строки
        title=${title:0:14}"..."
      fi

			if [ -n "$artist" ]; then
        if [ ${#artist} -gt 10 ]; then
          # Если да, берём первые 10 элементов и добавляем "..." в конец новой строки
          artist=${artist:0:14}"..."
        fi
				info_string="$artist - $title $info_string"
			else
				info_string="No - $title $info_string"
			fi

			if [ "$status" = "playing" ]; then
				echo " $info_string"
			elif [ "$status" = "paused" ]; then
				echo " $info_string"
			elif [ "$status" = "stopped" ]; then
				echo "󰎊 $info_string"
			else
				echo "Error list status!"
			fi
		else
			echo "Error no artist and title!"
		fi
	else
		echo "Error no status player!"
	fi
else
	echo "Offline cmus"
fi
